<?php
namespace App\Http\Controllers\ApiApp;

use App\DBs\UserDB;
use App\Utility\Upload;
use Illuminate\Http\Request;

class UserController extends BaseController
{
    /**
     * 用户
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new UserDB();
    }

    /**
     * 发送验证码
     */
    public function sendYzm(Request $request)
    {
        $phone = $request->post('phone',0);
        $resDb = $this->db->setYzm($phone);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-9,array(),$resDb['msg']);
        }
        return $this->toAppJson(0);
    }

    /**
     * 注册
     */
    public function doRegist(Request $request)
    {
        $phone = $request->post('phone',0);
        $pwd = $request->post('pwd',0);
        $yzm= $request->post('yzm',0);
        if (!$phone || !$pwd || !$yzm) {
            return $this->toAppJson(-2);
        }
        if (!preg_match('/^1[3456789]\d{9}$/',$phone)) {
            return $this->toAppJson(-11);
        }
        if (!preg_match('/[0-9a-zA-Z_]{6,20}/',$pwd)) {
            return $this->toAppJson(-12);
        }
        //验证验证码
        $resYzm = $this->db->getYzm($phone,$yzm);
        if (!$resYzm) {
            return $this->toAppJson(-14);
        }
        //判断用户
        $resDb = $this->db->getOneByPhone($phone);
        if ($resDb) {
            return $this->toAppJson(-13);
        }
        //新增用户
        $resDb2 = $this->db->setAdd($phone,$pwd);
        if (!$resDb2) {
            return $this->toAppJson(-15);
        }
        //新增登陆日志
        $resLogin = $this->db->setLoginAdd($phone);
        if (!$resLogin) {
            return $this->toAppJson(-16);
        }
        //返回用户信息
        $resDb3 = $this->db->getOneByPhone($phone);
        $dataArr = array(
            'uid' => $resDb3['id'],
            'name' => $resDb3['name'],
            'phone' => $resDb3['phone'],
            'head' => $resDb['head'],
            'appHead' => $resDb['localAppHead'],
            'headBg' => $resDb['headbg'],
            'appHeadBg' => $resDb['localAppHeadBg'],
            'detail' => $resDb['detail'],
            'sysType' => $resDb['sysType'],
            'userType' => $resDb['userType'],
        );
        return $this->toAppJson(0, $dataArr);
    }

    /**
     * 登陆
     */
    public function doLogin(Request $request)
    {
        $phone = $request->post('phone',0);
        $pwd = $request->post('pwd',0);
        if (!$phone || !$pwd) {
            return $this->toAppJson(-2);
        }
        if (!preg_match('/^1[3456789]\d{9}$/',$phone)) {
            return $this->toAppJson(-11);
        }
        if (!preg_match('/[0-9a-zA-Z_]{6,20}/',$pwd)) {
            return $this->toAppJson(-12);
        }
        //判断用户
        $resDb = $this->db->getOneByPhone($phone);
        if (!$resDb) {
            return $this->toAppJson(-16);
        }
        //新增登陆日志
        $resLogin = $this->db->setLoginAdd($phone);
        if (!$resLogin) {
            return $this->toAppJson(-16);
        }
        //返回用户信息
        $dataArr = array(
            'uid' => $resDb['id'],
            'name' => $resDb['name'],
            'phone' => $resDb['phone'],
            'head' => $resDb['head'],
            'appHead' => $resDb['localAppHead'],
            'headBg' => $resDb['headbg'],
            'appHeadBg' => $resDb['localAppHeadBg'],
            'detail' => $resDb['detail'],
            'sysType' => $resDb['sysType'],
            'userType' => $resDb['userType'],
        );
        return $this->toAppJson(0, $dataArr);
    }

    /**
     * 更新昵称
     */
    public function setNick(Request $request)
    {
        $uid = $request->post('uid',0);
        $nick = $request->post('nick','');
        $pwd = $request->post('pwd','');
        if (!$uid || !$nick || !$pwd) {
            return $this->toAppJson(-2);
        }
        if (!preg_match('/[0-9a-zA-Z_]{6,20}/',$pwd)) {
            return $this->toAppJson(-12);
        }
        //判断用户
        $resDb = $this->db->getOneByUid($uid);
        if (!$resDb) {
            return $this->toAppJson(-16);
        }
        if ($resDb['password']!=md5($pwd)) {
            return $this->toAppJson(-17);
        }
        $resDb2 = $this->db->setNick($uid, $nick);
        if (!$resDb2) {
            return $this->toAppJson(-100);
        }
        return $this->toAppJson(0);
    }

    /**
     * 更新密码
     */
    public function setPwd(Request $request)
    {
        $uid = $request->post('uid',0);
        $pwd = $request->post('pwd','');
        $pwd2 = $request->post('pwd2','');
        if (!$uid || !$pwd || !$pwd2) {
            return $this->toAppJson(-2);
        }
        if (!preg_match('/[0-9a-zA-Z_]{6,20}/',$pwd)) {
            return $this->toAppJson(-12);
        }
        if (!preg_match('/[0-9a-zA-Z_]{6,20}/',$pwd2)) {
            return $this->toAppJson(-12);
        }
        //判断用户
        $resDb = $this->db->getOneByUid($uid);
        if (!$resDb) {
            return $this->toAppJson(-16);
        }
        if ($resDb['password']!=md5($pwd2)) {
            return $this->toAppJson(-17);
        }
        $resDb2 = $this->db->setPwd($uid, md5($pwd));
        if (!$resDb2) {
            return $this->toAppJson(-100);
        }
        return $this->toAppJson(0);
    }

//    /**
//     * 上传头像
//     */
//    public function setHead(Request $request)
//    {
//        $uid = $request->post('uid', 0);
//        //判断用户
//        $resDb = $this->db->getOneByUid($uid);
//        if (!$resDb) {
//            return $this->toAppJson(-16);
//        }
//        $headOld = $resDb['head'];
//        //上传头像
//        $resUpload = Upload::uploadFile($request);
//        if (!$resUpload) {
//            return $this->toAppJson(-19);
//        }
//        $headNew = $resUpload['data']['filePath'];
//        //去除老头像
//        if ($headNew && $headOld && file_exists(trim($headOld,'/'))) {
//            unlink(trim($headOld,'/'));
//        }
////        return [$uid,$headNew];
//        //更新头像
//        $resDb2 = $this->db->setHead($uid, $headNew);
//        if (!$resDb2) {
//            return $this->toAppJson(-100);
//        }
//        $dataArr = array(
//            'head' => env('DOMAIN').$headNew,
//        );
//        return $this->toAppJson(0, $dataArr);
//    }

    /**
     * 上传头像
     */
    public function setHeadTmp(Request $request)
    {
//        echo "<pre>";
//        $stamp = isset($_POST['stamp']) ? $_POST['stamp'] : 0;
//        $files = isset($_FILES['file']) ? $_FILES['file'] : array();
//        print_r($stamp);
//        print_r($files);
//        exit;
        //上传标识：时间戳+随机值
        $stamp = isset($_POST['stamp']) ? $_POST['stamp'] : 0;
        //文件标识
        $files = isset($_FILES['file']) ? $_FILES['file'] : array();
        if (!$stamp || !$files) {
            return $this->toAppJson(-2);
        }
        //上传头像
        $resUpload = Upload::uploadAppFile($files);
        if (!$resUpload) {
            return $this->toAppJson(-19);
        }
        //文件临时数据设置
        $resDb = $this->db->setUploadFileTmp($stamp, $resUpload);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-20);
        }
        $dataArr = $resDb['data'];
        return $this->toAppJson(0, $dataArr);
    }

    /**
     * 上传更新
     */
    public function setHeadUpdate(Request $request)
    {
        $stamp = $request->post('stamp', 0);
        $uid = $request->post('uid', 0);
        $path = $request->post('path', '?'); //用户端地址
        if (!$stamp || !$uid) {
            return $this->toAppJson(-2);
        }
        //文件临时数据更新
        $resDb = $this->db->setHeadUpdate($stamp, $uid, $path);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-20);
        }
        return $this->toAppJson(0);
    }

    /**
     * 上传头像
     */
    public function setHeadBgTmp(Request $request)
    {
        //上传标识：时间戳+随机值
        $stamp = isset($_POST['stamp']) ? $_POST['stamp'] : 0;
        //文件标识
        $files = isset($_FILES['file']) ? $_FILES['file'] : array();
        if (!$stamp || !$files) {
            return $this->toAppJson(-2);
        }
        //上传头像
        $resUpload = Upload::uploadAppFile($files);
        if (!$resUpload) {
            return $this->toAppJson(-19);
        }
        //文件临时数据设置
        $resDb = $this->db->setUploadFileTmp($stamp, $resUpload);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-20);
        }
        $dataArr = $resDb['data'];
        return $this->toAppJson(0, $dataArr);
    }

    /**
     * 上传更新
     */
    public function setHeadBgUpdate(Request $request)
    {
        $stamp = $request->post('stamp', 0);
        $uid = $request->post('uid', 0);
        $path = $request->post('path', '?'); //用户端地址
        if (!$stamp || !$uid) {
            return $this->toAppJson(-2);
        }
        //文件临时数据更新
        $resDb = $this->db->setHeadBgUpdate($stamp, $uid, $path);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-20);
        }
        return $this->toAppJson(0);
    }
}